/*
 * Created on Jun 13, 2004
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * http//www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is MAPONTO.
 *
 * The Initial Developer of the Original Code is University of Toronto. Portions
 * created by University of Toronto are Copyright (C) 2004.  All Rights Reserved.
 *
 * MAPONTO was developed by Knowledge Management Lab
 * (http//www.cs.toronto.edu/km) at the University of Toronto Department of 
 * Computer Science. Current information about MAPONTO can be obtained at 
 * http//resi.ai.toronto.edu:8080
 *
 */

/*
 * Contributor(s): Yuan An yuana@cs.toronto.edu
 */
package main.naiveBayes.tests;

import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.log4j.Logger;

import y.base.Edge;

import main.graph.FormTreeWithMapping;
import main.graph.GetCMGraph;
import main.graph.GetFormTreeWithMapping;
import main.graph.ValuedGraph;
import main.logger.LoggerSetup;
import main.model.EdgeGivenPath;
import main.model.EdgePriors;
import main.model.OntoEle;
import main.model.OntoPath;
import main.naiveBayes.LearnParameter;
import edu.stanford.smi.protegex.owl.ProtegeOWL;
import edu.stanford.smi.protegex.owl.model.OWLIndividual;
import edu.stanford.smi.protegex.owl.model.OWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;

public class LearnParameterTests extends TestCase {

	static {
		LoggerSetup.enable(); // Enable log4j logging
	}

	static Logger log = Logger.getLogger(LearnParameterTests.class);

	/**
	 * Constructor
	 * 
	 * @param name
	 */
	public LearnParameterTests(String name) {
		super(name);

	}

	/**
	 * Set-up is always called first by the JUnit test framework.
	 */
	protected void setUp() throws Exception {
		super.setUp();

	}

	/**
	 * NOTE: Tests are identified by JUnit by methods starting with the name
	 * test. The order that JUnit executes test cases is to call, sequentially:
	 * 
	 * setUp() testXXXXX() // test 1 testXXXXX() // test 2 tearDown()
	 * 
	 */

	public void testLearnParameter() {
		
		try{
			File file = new File("ontologies/ns-v1.owl");
			
			ValuedGraph cmg = GetCMGraph.getGraph(file);
			
			String fileName = "resources/formTreesx5.graph.mapping";
			
			ArrayList<FormTreeWithMapping> trees = GetFormTreeWithMapping.getFormTrees(fileName, cmg);
				
			System.out.println(GetFormTreeWithMapping.displayFormTrees(trees,cmg));
			
			//learn priors
			PrintWriter priorOutput = new PrintWriter(new File("ontologies/prior.output"));
			HashMap<OntoEle, Double> edgePrior = LearnParameter.learnPrior(trees, cmg);
			
			
			Iterator<OntoEle> eit = edgePrior.keySet().iterator();
			while(eit.hasNext()){
				OntoEle ele = eit.next();
				Double prior = edgePrior.get(ele);
				priorOutput.println(ele.display() + " :=: " + prior);
				System.out.println(ele.display() + " :=: " + prior);
			}
			
			//display the priors in terms of ontology edges
			EdgePriors priors = LearnParameter.learnEdgePrior(trees, cmg);
			HashMap<Edge, Double> priorMaps = priors.getPriors();
			
			System.out.println("=====================PRIORS IN EDGES=====================");
			priorOutput.println("=====================PRIORS IN EDGES=====================");
			
			Iterator<Edge> edgeit = priorMaps.keySet().iterator();
			while(edgeit.hasNext()){
				Edge edge = edgeit.next();
				Double prior = priorMaps.get(edge);
				priorOutput.println(edge.toString() + " :=: " + prior);
				System.out.println(edge.toString() + " :=: " + prior);
			}
			System.out.println("The prior for all other edges:" + priors.getDummyPrior());
			priorOutput.println("The prior for all other edges:" + priors.getDummyPrior());
			
			
			priorOutput.flush();
			
			System.out.println("=====================LIKELIHOOD=====================");
			
			//learn likelihood
			PrintWriter likelihoodOutput = new PrintWriter(new File("ontologies/likelihood.output"));
			ArrayList<ArrayList<EdgeGivenPath>> pathCount = LearnParameter.learnLikelihood(trees, cmg);
			
			for(ArrayList<EdgeGivenPath> edges: pathCount){
				for(EdgeGivenPath edge: edges){
					String line = edge.display() + " :=: " + edge.getConditionalProb();
					
					String linePathEdges = edge.displayPathEdges() + " :=: " + edge.getConditionalProb();
					
					System.out.println(line);
					System.out.println(linePathEdges);
					likelihoodOutput.println(line);
					likelihoodOutput.println(linePathEdges);
				}
			}
			
			likelihoodOutput.flush();
		}
		catch(Exception e){
			e.printStackTrace();
		}
			
	}

	public static Test suite() {
		TestSuite suite = new TestSuite();
		
		suite.addTest(new LearnParameterTests("testLearnParameter"));

		return suite;
	}

	/**
	 * tearDown() (does nothing)
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * The main routine
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		junit.textui.TestRunner.run(suite());
	}
}
